#include "uart.h"
#define UART0       0x10000000UL
#define REG8(a)     (*(volatile uint8 *)(a))//将地址 a 视为指向 8 位硬件寄存器的指针并解引用。
#define UART_THR    (UART0 + 0x00)   // transmit holding
#define UART_RHR    (UART0 + 0x00)   // receive holding
#define UART_LSR    (UART0 + 0x05)   // line status
#define LSR_TX_IDLE (1 << 5) //THRE，1表示可以发送下一个字符，0表示发送缓冲区满
#define LSR_RX_READY (1 << 0) // Data ready

void uart_init(){}

void uart_putc(char c) {
  while ((REG8(UART_LSR) & LSR_TX_IDLE) == 0) { }
  REG8(UART_THR) = (uint8)c;
}

void uart_puts(const char *s) {
  while (*s) {
    if (*s == '\n') uart_putc('\r');
    uart_putc(*s++);
  }
}

int uart_getc(void) {
  while ((REG8(UART_LSR) & LSR_RX_READY) == 0) {
    // busy wait for data
  }
  return REG8(UART_RHR);
}